package core.rendering;

import java.awt.BufferCapabilities;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import utils.DBG;
import utils.Properties;

public class ScreenManager {
	private static GraphicsConfiguration gConf;
	private static GraphicsDevice gDevice;
	/* static initilizer */
	static {
		try {
			gDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
					.getDefaultScreenDevice();
			gConf = gDevice.getDefaultConfiguration();

			DBG.showMsg(" ColorModel:" + gConf.getColorModel(), null);
			DBG.showMsg(" Device :" + gDevice, null);
			DBG.showMsg(" AvailableAcceleratedMemory:"
					+ gDevice.getAvailableAcceleratedMemory(), null);
			DBG.log(" GraphicsConfiguration:" + gConf);
			BufferCapabilities bc = gConf.getBufferCapabilities();
			DBG.log(" BufferCapabilities:" + bc);
			DBG.log(" FullScreenRequired:" + bc.isFullScreenRequired());
			DBG.log(" Page Flipping :" + bc.isPageFlipping());
			DBG.log(" MultiBufferAvailable:" + bc.isMultiBufferAvailable());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static ScreenManager instance = new ScreenManager();

	// private static ScreenManager getInstance() {
	// return instance;
	// }

	public static boolean goToFullMode(JFrame frame) {
		boolean result;
		frame.setUndecorated(true);
		//        
		// pone la pantalla fullscreen.
		// 

		int i;
		int width = Properties.SCREEN_WIDTH;
		int height = Properties.SCREEN_HEIGHT;

		DisplayMode currentDisplayMode = gDevice.getDisplayMode();
		DisplayMode[] availableDisplayModes = gDevice.getDisplayModes();

		int depth = -1;
		int refreshRate = 60;
		for (i = 0; i < availableDisplayModes.length; i++) {
			if (availableDisplayModes[i].getWidth() == width
					&& availableDisplayModes[i].getHeight() == height) {
				/* found the proper depth */
				int modeDepth = availableDisplayModes[i].getBitDepth();
				if (modeDepth > depth) {
					depth = -1;
					if (availableDisplayModes[i].getRefreshRate() >= 75) {
						refreshRate = 75;
					}
				}
			}
		}

		if (depth == -1) {
			depth = currentDisplayMode.getBitDepth();
		}

		DisplayMode displayMode = new DisplayMode(width, height,
				currentDisplayMode.getBitDepth(), refreshRate);
		
		try {

			if (gDevice.isFullScreenSupported()) {
				gDevice.setFullScreenWindow(frame);
				
				gDevice.setDisplayMode(displayMode);
				result = true;
				DBG.showMsg(" FullScreenMode seems to be correctly set",
						ScreenManager.class);
			} else {
				DBG
						.showMsg(
								" FulLScreenMode unsuccessfull -> operation non supported",
								ScreenManager.class);
				result = false;
			}
		} catch (Exception e) {
			DBG.showMsg(" Exception during go to fullscreenmode\n",
					ScreenManager.class);
			result = false;

		}

		return result;

		// DisplayMode displayMode = new DisplayMode(GameProperties.scrWidth,
		// GameProperties.scrHeight, 16,60);
		// GraphicsEnvironment environment = GraphicsEnvironment
		// .getLocalGraphicsEnvironment();
		// GraphicsDevice device = environment.getDefaultScreenDevice();
		// device.setFullScreenWindow(frame);
		// device.setDisplayMode(displayMode);
		// return true;
	}

	private Image createBI(int width, int height, int transparency) {
		BufferedImage img = gConf.createCompatibleImage(width, height,
				transparency);

		return img;

	}

	public static BufferCapabilities getBufferCapabilities() {
		BufferCapabilities bc;

		bc = gConf.getBufferCapabilities();
		return bc;

	}

	public static Image createBufferedImage(int width, int height,
			int transparency) {
		return instance.createBI(width, height, transparency);
	}

	public static Image createBufferedImage(int width, int height) {
		return instance.createBI(width, height, BufferedImage.TRANSLUCENT);
	}

}
